// pages/survey/survey.js
const app = getApp();
import WxCanvas from '../../ec-canvas/wx-canvas';
import * as echarts from '../../ec-canvas/echarts';
let chart = null;


function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: 300
  });
  canvas.setChart(chart);
    setTimeout(function () {
      //console.log("dataList")
      var dataList = wx.getStorageSync('dataList');
      //console.log(dataList)
      var option = {
        color: ["#6a87c8"],
        legend: {
          show: true, //写成false不行
        },
        grid: {
          containLabel: true
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
          axisLabel: {
            interval: 0,
            textStyle: {
              color: "#666", //刻度颜色
            },
          }
        },
        yAxis: {
          x: 'center',
          type: 'value',
          axisTick: { show: false },
          splitLine: {
            lineStyle: {
              type: 'dashed'
            }
          },
          axisLine: {
            lineStyle: {
              color: '#999'
            }
          },
          axisLabel: {
            color: '#666'
          }
        },
        series: [{
           name: '年度走势图',
           type: 'line',
           smooth: true,
           areaStyle: {},
          data: dataList,
          label: {
            normal: {
              rich: {}
            }
          }
        }]
      };
     
      chart.setOption(option);
      return chart;
    }, 2000)
      
}


Page({
  data: {
    ec: {
      onInit: initChart
    },
    dataList:[]

  },
  
  goRealtimeTap:function(event){
    console.log(event)
    var that = this;
    var uid = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../realtime/realtime?id=' + uid + '&name=' + name,
    })
  },
  goIndexTap:function(){
    wx.navigateTo({
      url: '../index/index',
    })
  },
  /**
   * 页面的初始数据
   */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    // 获取游戏产品ID和名称 获取贴纸详情
    var uid = options.id;
    var name = options.name;
    _this.setData({
      uid: uid,
      name: name
    })
    this.requestAPI(uid); 

  },
  requestAPI:function(uid){
    var _this = this;
    wx.request({
      url: 'https://data1.cqhot.cn/index.php?s=/Admin/Apiwx/info',
      data: {
        user_id: uid
      },
      success: function (res) {
        console.log(res.data.data);
        wx.clearStorage({
          success: function (res) {
            console.log(res+"清除缓存成功！")
          }
        })
        _this.setData({
          datetime: res.data.data.total.date,
          amount: res.data.data.total.amount,
          amount_ratio: res.data.data.total.amount_ratio,
          finish: res.data.data.total.finish,
          finish_ratio: res.data.data.total.finish_ratio,
          making: res.data.data.total.making,
          making_ratio: res.data.data.total.making_ratio,
          dataList: res.data.data.chart

        })

        if (res.data.data.user.portrait == null) {
          _this.setData({
            portrait: "../../images/headimg.jpg",
          })
        } else {
          _this.setData({
            portrait: res.data.data.user.portrait,
          })
        }
       
        var dataList = res.data.data.chart
        wx.setStorageSync('dataList', dataList);//缓存图表数据


      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: 'CRM助手',
      path: '/pages/survey/survey',
      success: function () { },
      fail: function () { }
    }
  },
})